<link  href="/stylesheets/add.css" type="text/css" rel="stylesheet" media="screen">
<p style="margin: 5px 0pt;"><%=link_to  '测试管理' , :controller=>"surveys" ,:action=>"index"%> >>
<%=link_to '倾向测试管理' ,:controller=>"t_tests_manage" , :action=>"index"%> >> 添加题目 </p><br  />
<br/>
<h2>添加题目</h2>
<div id="questionsDiv">
    <!--题目列表--->
    <%= render :partial => "questions", :object => @part %>
  </div>
  <!-- quesiton添加表单-->
  <div id="add_form_div" style="margin-left:0px"><br/>
    <form id="form0" action="/surveys/t_tests_manage/create_question" class="required-validate">
      <input type="hidden" value="<%=@test.id%>" name="t_id"/>
      输入题目标题:<input type="test" size="20" name="question[title]" class="required"><br />
      输入题目描述:<br /><textarea cols="40" rows="4" name="question[description]" class="required"></textarea><br/>
      请输入加权系数:<input type="text" name="question[coefficient]"  
          class="required validate-integer" size="10" style="width:100px"/>
      <a href="javascript:;" onclick="addChoice($('form0'), 'choice0');">添加选项</a><br />
       选项<input type="text" name="choice.content[]"  class="required"/>&nbsp;
       分值<input type="text" name="choice.value[]" class="required validate-integer"/><br />
       选项<input type="text" name="choice.content[]" class="required"/>&nbsp;
       分值<input type="text" name="choice.value[]" class="required validate-integer"/><br/>
    </form>
    <input type="button" value="保存" onclick="saveQuestion($('form0'));" class="cButton" style="margin-left: 100px"/>
     <input type="button" value="完成" onclick="back();" class="cButton" style="margin-left: 20px"/>
  </div>
  <!-- quesiton添加表单结束 -->
</div>
<script type="text/javascript">

     function back(){
        window.location.href="/surveys/surveys/"; 
     }
//变量声明
var question_id = 0;
var choice_num = 2;

//给题目添加一个选项
function addChoice(parent, id) {
  parent.appendChild(createChoice(id, true));
}

//创建一个选项
function createChoice(id, isRemove) {
  var choiceDiv = document.createElement("div");
  var a = createA("javascript:;", "<font color=red>删除</font>")
  a.onclick = function(){ removeElement(choiceDiv) }

  choiceDiv.id = id;
  choiceDiv.appendChild(createLabel("选项"));
  choiceDiv.appendChild(createTextField("choice.content[]","required"));

  choiceDiv.appendChild(createLabel("&nbsp;&nbsp;"));

  choiceDiv.appendChild(createLabel("分值"));
  choiceDiv.appendChild(createTextField("choice.value[]","required validate-integer"));

  if(isRemove){
    choiceDiv.appendChild(a);
  }

  choice_num++;
  return choiceDiv;
}

//删除自身
function removeElement(el){
  el.parentNode.removeChild(el);
}

//创建textfield
function createTextField(name,class1) {
  var newInput = document.createElement("input");
  newInput.type = "text";
  newInput.name = name;
  newInput.class=class1;
  return newInput;
}

//创建换行符
function br(){
  return document.createElement("br");
}

//创建文本
function createLabel(content){
  var label = document.createElement("span");
  label.innerHTML = content;
  return label;
}

//创建链接
function createA(href, content) {
  var a = document.createElement("a");
  a.href = href;
  a.innerHTML = content;
  return a;
}

//创建select
function createSelect(name){
  var select = document.createElement("select");
  select.name = name;
  select.appendChild(createOption("<%=TEST_TYPE_TREND%>", "<%=TEST_TYPE_TREND%>"));
  select.appendChild(createOption("<%=TEST_TYPE_JUMP%>", "<%=TEST_TYPE_JUMP%>"));
  select.appendChild(createOption("<%=TEST_TYPE_COUNT%>", "<%=TEST_TYPE_COUNT%>"));
}

//创建option
function createOption(value, content){
  var option = document.createElement("option");
  option.value = value;
  option.innerHTML = content;
  return option;
}

//创建按钮
function　createInput(value, type) {
  var input = document.createElement("input");
  input.value = value;
  input.type = type;
  return input;
}

//异步保存题目
function saveQuestion(form) {
    new Ajax.Updater(
      "questionsDiv",
      form.action,
      {
        method: "post",
        asynchronous: true,
        evalScripts: true,
        parameters: Form.serialize(form) + '&authenticity_token=' + window._token
      }
    );
}

//删除所有子节点
function removeAllNodes(parentNode){
  while (parentNode.firstChild) {
    var oldNode = parentNode.removeChild(parentNode.firstChild);
    oldNode = null;
  }
}

window.onload = function() {
}
</script>
